﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace Consumer
{
    class Program
    {
        static void Main(string[] args)
        {
            ServeLutronRequests();
        }

        private static void PullModel()
        {
            var connectionFactory = new ConnectionFactory();
            using (IConnection connection = connectionFactory.CreateConnection())
            {
                using (IModel channel = connection.CreateModel())
                {
                    channel.QueueDeclare("hello-world-queue", false, false, false, null);

                    while (true)
                    {
                        Console.WriteLine("Enter for next message or x to exit");
                        string resposne = Console.ReadLine();
                        if (resposne == "x") break;
                        BasicGetResult result = channel.BasicGet("hello-world-queue", true);
                        if (result != null)
                        {
                            string message = Encoding.UTF8.GetString(result.Body);
                            Console.WriteLine(message);
                        }

                    }
                }
            }
        }

        private static void PushModel()
        {
            var connectionFactory = new ConnectionFactory();
            using (IConnection connection = connectionFactory.CreateConnection())
            {
                using (IModel channel = connection.CreateModel())
                {
                    channel.QueueDeclare("hello-world-queue", false, false, false, null);
                    var consumer = new QueueingBasicConsumer(channel);
                    
                    channel.BasicConsume("hello-world-queue", true, consumer);
                    while (true)
                    {
                        var eventArgs = (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                        string message = Encoding.UTF8.GetString(eventArgs.Body);
                        Console.WriteLine(message);

                    }
                }
            }
        }

        private static void ServeLutronRequests()
        {
            var connectionFactory = new ConnectionFactory();
            using (IConnection connection = connectionFactory.CreateConnection())
            {
                using (IModel channel = connection.CreateModel())
                {
                    var consumer = new QueueingBasicConsumer(channel);
                    channel.BasicConsume("request", true, consumer);
                    while (true)
                    {
                        var eventArgs = (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                        string message = Encoding.UTF8.GetString(eventArgs.Body);
                        Console.WriteLine(message);
                        channel.BasicPublish("lutron.quantum", "project.1.response.1", null, eventArgs.Body);
                    }
                }
            }
        }

    }
}
